<template>

  <div class="app-container">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input clearable v-model="hospitalSetQueryVo.hosname" placeholder="医院名称"/>
      </el-form-item>
      <el-form-item>
        <el-input clearable v-model="hospitalSetQueryVo.hoscode" placeholder="医院编号"/>
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
    </el-form>

    <div>
      <el-button type="primary" size="mini" icon="el-icon-plus" @click="add(0)">添加医院设置</el-button>
      <el-button type="danger" icon="el-icon-delete" size="mini" @click="batchRemove()">批量删除</el-button>
    </div>
    <el-table
      :data="list"
      stripe
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"/>
      <el-table-column prop="id" label="id" width="50"/>
      <el-table-column prop="hosname" label="医院名称"/>
      <el-table-column prop="hoscode" label="医院编号"/>
      <el-table-column prop="apiUrl" label="api基础路径" width="150"/>
      <el-table-column prop="contactsName" label="联系人姓名"/>
      <el-table-column prop="contactsPhone" label="联系人手机号码"/>
      <el-table-column label="状态" width="80">
        <template slot-scope="scope">
          {{scope.row.status===1?'可用':'不可用'}}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="350" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="add(0,scope.row.id)">
            编辑
          </el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete"
                     @click="removeById(scope.row.id)">删除
          </el-button>
          <el-button v-if="scope.row.status==1" type="primary" size="mini"
                     icon="el-icon-lock" @click="lockHospitalSet(scope.row.id,0)">
            锁定
          </el-button>
          <el-button v-else type="primary" size="mini"
                     icon="el-icon-unlock" @click="lockHospitalSet(scope.row.id,1)">
            取消锁定
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="fetchData"
      :current-page="page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style="padding: 30px 0; text-align: center;"
    />

    <!--下面是新增数据-->
    <el-dialog :title="title" :visible.sync="dialogVisible" width="40%" @before-close="closeModal">
      <span>
        <el-form ref="form" :model="hospitalSet" label-width="95px">
          <el-form-item label="id" hidden>
            <el-input v-model="hospitalSet.id"/>
          </el-form-item>
          <el-form-item label="医院名称">
            <el-input v-model="hospitalSet.hosname"/>
          </el-form-item>
          <el-form-item label="医院编号">
            <el-input v-model="hospitalSet.hoscode"/>
          </el-form-item>
          <el-form-item label="api基础路径">
            <el-input v-model="hospitalSet.apiUrl"/>
          </el-form-item>
          <el-form-item label="联系人姓名">
            <el-input v-model="hospitalSet.contactsName"/>
          </el-form-item>
          <el-form-item label="联系人手机">
            <el-input v-model="hospitalSet.contactsPhone"/>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeModal">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdate">确 定</el-button>
      </span>
    </el-dialog>


  </div>
</template>

<script>
  import hospitalSetApi from '@/api/hosp/hospitalSet'

  export default {
    name: 'List',
    data() {
      return {
        dialogVisible: false,
        title: '添加医院设置',
        page: 1,
        limit: 10,
        hospitalSetQueryVo: {},
        list: [],
        total: 0,
        multipleSelection: [], // 批量选择中选择的记录列表
        hospitalSet: {
          id: '',
          hosname: '',
          hoscode: '',
          apiUrl: '',
          contactsName: '',
          contactsPhone: '',
        },
      }
    },

    created() {
      this.fetchData(1)
    },
    methods: {
      closeModal() {
        this.hospitalSet = {}
        this.dialogVisible = false
      },
      add(key, id) {
        if (id === undefined) {
          this.title = '添加'
          this.dialogVisible = true
        } else {
          this.title = '编辑'
          hospitalSetApi.getHospSet(id).then(response => {
            this.hospitalSet = response.data
          })
          this.dialogVisible = true
        }
      },
      //当表格复选框选项发生变化的时候触发
      handleSelectionChange(selection) {
        this.multipleSelection = selection
      },
      handleSizeChange(val) {
        this.limit = val
        this.fetchData(1);
      },
      //加载列表
      fetchData(page=1) {
        this.page = page
        hospitalSetApi.postPageList(this.page, this.limit, this.hospitalSetQueryVo).then(
          response => {
            this.list = response.data.records
            //总记录数
            this.total = response.data.total
          }
        )
      },
      //根据id删除
      removeById(id) {
        this.$confirm('此操作将永久删除医院是设置信息, 是否继续?, 提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          hospitalSetApi.removeById(id).then(response => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
          this.fetchData(1)
        })
      },
      //批量删除
      batchRemove() {
        this.$confirm('此操作将永久删除医院是设置信息, 是否继续?, 提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          var idList = []
          for (var i = 0; i < this.multipleSelection.length; i++) {
            var obj = this.multipleSelection[i]
            var id = obj.id
            idList.push(id)
          }
          hospitalSetApi.batchRemove(idList).then(response => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
          this.fetchData(this.page)
        })
      },

      //锁定和取消锁定
      lockHospitalSet(id, status) {
        hospitalSetApi.lockHospitalSet(id, status).then(response => {
          this.fetchData(this.page)
        })
      },

      saveOrUpdate() {
        if (this.hospitalSet.id) {
          debugger
          this.updateHospitalSet()
        } else {
          debugger
          this.saveHospSet()
        }
      },

      updateHospitalSet() {
        hospitalSetApi.updateHospitalSet(this.hospitalSet).then(response => {
          this.$message({
            type: 'success',
            message: '编辑成功!'
          })
          this.closeModal()
          this.fetchData(1)
        })
      },

      saveHospSet() {
        hospitalSetApi.saveHospSet(this.hospitalSet).then(response => {
          this.$message({
            type: 'success',
            message: '添加成功!'
          })
          this.closeModal()
          this.fetchData(1)
        })
      },
    },
  }
</script>


<style scoped>

</style>
